package com.dylan.netty.nio.socket;

import lombok.extern.slf4j.Slf4j;

import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;

/**
 * @author zhangshaojian
 * @date 2022/8/10 9:50
 */
@Slf4j
public class Client {
    public static void main(String[] args) {

        try (
                SocketChannel channel = SocketChannel.open()
        ) {
            channel.configureBlocking(false);
            if (!channel.connect(new InetSocketAddress(8888))) {
                while (!channel.finishConnect()) {
                    log.info("服务器连接中。。。。。。");
                }
            }
            log.info("连接服务器成功！");
            Scanner scanner = new Scanner(System.in);
            while (true) {
                String content = scanner.next();
                log.info("输入：{}", content);
                ByteBuffer buffer = ByteBuffer.wrap(content.getBytes(StandardCharsets.UTF_8));
                channel.write(buffer);
            }

        } catch (Exception ex) {

        }
    }
}
